package Aleehot100;

/**
 * ClassName: c088
 * Package: Aleehot100
 * Description:152. 乘积最大子数组
 *
 * @Author BCXJ
 * @Create 2025/9/13 18:04
 * @Version 1.0
 * @Since 1.0
 */
public class c088 {
    public static void main(String[] args) {

    }


    public static int maxProduct(int[] nums) {
        if(nums.length == 0) return 0;

        int maxRes = nums[0];
        int curMax = nums[0];
        int curMin = nums[0];

        // 遍历元素
        for(int i = 1; i < nums.length; i++) {
            // 为负数的时候，交换大小值
            if(nums[i] < 0) {
                int temp = curMax;
                curMax = curMin;
                curMin = temp;
            }

            curMax = Math.max(curMax * nums[i], nums[i]);
            curMin = Math.min(curMin * nums[i], nums[i]);

            maxRes = Math.max(maxRes, curMax);

        }

        return maxRes;
    }

}
